//
//  FSWTabbar.m
//  quhuolang_driver
//
//  Created by 曹欣欣 on 2018/3/1.
//  Copyright © 2018年 fusw. All rights reserved.
//

#import "FSWTabbar.h"
@implementation FSWTabbar {
    FSWTabbarButton *_selectedBarButton;
    
}
-(void) buttonClick:(FSWTabbarButton *)button{
    
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addBarButtons];
    }
    return self;
}

-(void) addBarButtons{
    
     ;
    
    NSArray *titlesArr = @[@"聊天",@"应用",@"通讯录",@"我的"];
    NSArray *normalImage = @[@"tabbr_chat_gray" ,@"tabbar_apply_gray" ,@"tabbar_contacts_gray" ,@"tabbr_mine_gray"];
    NSArray *selectedImage = @[@"tabbr_chat_red" ,@"tabbar_apply_red" ,@"tabbar_contacts_red" ,@"tabbr_mine_red" ];
    
    
    
    for (int i = 0 ; i<titlesArr.count ; i++) {
        FSWTabbarButton *btn = [[FSWTabbarButton alloc] init];
        CGFloat btnW = self.frame.size.width/titlesArr.count;
        CGFloat btnX = i * btnW;
        CGFloat btnY = 0;
        CGFloat btnH = self.frame.size.height;
        
        btn.frame = CGRectMake(btnX, btnY, btnW, btnH);
        NSString *imageName = [NSString stringWithFormat:@"%@",normalImage[i]];
        NSString *selImageName = [NSString stringWithFormat:@"%@",selectedImage[i]];
        NSString *title = titlesArr[i];
        
        [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:selImageName] forState:UIControlStateSelected];
        btn.tag = i;
        
        
//        if (i!=2) {
            [btn setTitle:title forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize: 13.0];
            btn.titleLabel.textAlignment = NSTextAlignmentCenter;
            [btn setTitleColor:XCMainColor forState:UIControlStateSelected];
            [btn setTitleColor:[UIColor colorWithHexString:@"333333"] forState:UIControlStateNormal];
            [self addSubview:btn];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
//        }
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        //        [self addSubview:btn];
        
        if(i == 1){
            [self btnClick:btn];
        }
    }
}


-(void) btnClick:(FSWTabbarButton *)button{
    
    
    [self.delegate changeNav:_selectedBarButton.tag to:button.tag andBtn:button];
    _selectedBarButton.selected = NO;
    button.selected = YES;
    _selectedBarButton = button;
    
    
}

@end
